package com.example.demo.controlller;

import com.example.demo.entity.Userinfo;
import com.example.demo.service.LoginService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @author krystal
 * @date 2023/3/11 9:09
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private DataSourceTransactionManager transactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;
    @Autowired
    private LoginService loginService;

    @RequestMapping("/add")
    public int add(Userinfo userinfo){
        //非空效验
        if (userinfo==null || !StringUtils.hasLength(userinfo.getUsername())
            || !StringUtils.hasLength(userinfo.getPassword())){
            return 0;
        }
        //1、开启事务
        TransactionStatus transactionStatus=transactionManager.getTransaction(transactionDefinition);

        //手动设置创建时间和修改时间的默认值
        userinfo.setCreatetime(LocalDateTime.now().toString());
        userinfo.setUpdatetime(LocalDateTime.now().toString());

        int result= userService.add(userinfo);
        System.out.println("添加："+result);

        //2、回滚事务
        //transactionManager.rollback(transactionStatus);

        //3、提交事务
        transactionManager.commit(transactionStatus);
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED)  //声明式事务（自动提交）
    @RequestMapping("insert")
    public Integer insert(Userinfo userinfo){
        //非空效验
        if (userinfo==null || !StringUtils.hasLength(userinfo.getUsername())
                || !StringUtils.hasLength(userinfo.getPassword())){
            return 0;
        }
        //添加用户
        int result= userService.add(userinfo);
        if (result>0){
            //日志
            loginService.add();
        }
//        System.out.println("添加："+result);
//        try {
//            int num=10/0;
//        } catch (Exception e) {
//            //1、将异常继续抛出
//            //throw e;
//            System.out.println(e.getMessage());
//            //2、使用代码手动回滚事务
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }
        return result;
    }
}
